package com.kk.init;

import com.kk.model.*;
import com.kk.model.enumbean.LevelEnum;
import com.kk.model.enumbean.RoleEnum;
import com.kk.repository.*;
import com.kk.util.SetUtils;
import org.hibernate.annotations.common.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author: kevin
 * @data: 2018-08-16 09:03
 * @desc: 系统初始化数据
 */
@Component
@Order(value = 1)
public class MyApplicationRunner implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(MyApplicationRunner.class);

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private PermissionRepository permissionRepository;
    @Autowired
    private MenuRepository menuRepository;
    @Autowired
    private RoleMenuRelRepository roleMenuRelRepository;

    @Value("${spring.jpa.hibernate.ddl-auto}")
    private String ddl_auto;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        logger.info("===服务启动后，初始化数据等操作===");
        if (StringHelper.isNotEmpty(ddl_auto) && "create".equals(ddl_auto)) {
            Set<User> users = new HashSet<>();
            for (int i = 0; i < 100; i++) {
                User user = new User("小倩" + i, "123456");
                user.setSort(i);
                Set<Role> roles = new HashSet<>();
                Role role1 = new Role(RoleEnum.SUPERADMIN.name());
                Role role2 = new Role(RoleEnum.USER.name());
//            roles.addAll(SetUtils.addAll(role1, role2));
                roles = SetUtils.addAll(role1, role2);
                Permission permission1 = new Permission("addUser");
                Permission permission2 = new Permission("deleteUser");
                Permission permission3 = new Permission("updateUser");
                Permission permission4 = new Permission("selectUser");
                Set<Permission> permissionSet = new HashSet<>();
                permissionSet = SetUtils.addAll(permission1, permission2, permission3, permission4);
                Set<Permission> permissionSet1 = new HashSet<>();
                permissionSet1 = SetUtils.addAll(permission4);
                role1.setPermissions(permissionSet);
                role2.setPermissions(permissionSet1);
                user.setRoles(roles);
                users.add(user);
            }
            userRepository.saveAll(users);
            User user1 = new User("小凯", "123456");
            User user2 = new User("小倩", "123456");
            User user3 = new User("小仙女", "123456");
            User user4 = new User("小宝贝", "123456");
            User user5 = new User("小明", "123456");
            User user6 = new User("小美", "123456");

            Set<Role> roles = new HashSet<>();
            Role role1 = new Role(RoleEnum.SUPERADMIN.name());
            Role role2 = new Role(RoleEnum.USER.name());
//            roles.addAll(SetUtils.addAll(role1, role2));
            roles = SetUtils.addAll(role1, role2);
            Permission permission1 = new Permission("addUser");
            Permission permission2 = new Permission("deleteUser");
            Permission permission3 = new Permission("updateUser");
            Permission permission4 = new Permission("selectUser");
            Set<Permission> permissionSet = new HashSet<>();
            permissionSet.add(permission1);
            permissionSet.add(permission2);
            permissionSet.add(permission3);
            permissionSet.add(permission4);
            role1.setPermissions(permissionSet);
            Set<Permission> permissionSet1 = new HashSet<>();
            permissionSet1.add(permission4);
            role2.setPermissions(permissionSet1);
            Set<Role> roleSet = new HashSet<>();
            roleSet.add(role1);
            Set<Role> roleSet1 = new HashSet<>();
            roleSet1.add(role2);
            user1.setRoles(roles);
            user2.setRoles(roleSet);
            user3.setRoles(roleSet);
            user4.setRoles(roleSet);
            user5.setRoles(roleSet);
            user6.setRoles(roleSet1);
            Set<User> userSet = new HashSet<>();
            userSet.add(user1);
            userSet.add(user2);
            userSet.add(user3);
            userSet.add(user4);
            userSet.add(user5);
            userSet.add(user6);
            userRepository.saveAll(userSet);
            //设置菜单
            Set<Menu> menus = new HashSet<>();
            MenuMeta meta1 = new MenuMeta(true, true, null, null);
            MenuMeta meta11 = new MenuMeta(true, true, "首页", null);
            MenuMeta meta2 = new MenuMeta("系统管理", "_bear");
            MenuMeta meta3 = new MenuMeta("数据分析", "_bear");
            MenuMeta meta4 = new MenuMeta("个人中心", "_bear");
            MenuMeta meta5 = new MenuMeta("数字渐变", "_bear");
            MenuMeta meta6 = new MenuMeta("多功能表格", "_bear");
            MenuMeta meta7 = new MenuMeta("分割窗口", "_bear");
            MenuMeta meta8 = new MenuMeta("Markdown编辑器", "_bear");
            Menu menu1 = new Menu("首页", LevelEnum.ONE, LevelEnum.ONE.getDescription(), "/", "_home", meta1);
            menu1.setRedirect("/home");
            Menu menu2 = new Menu("系统管理", LevelEnum.ONE, LevelEnum.ONE.getDescription(), "/components", "components", meta2);
            Menu menu3 = new Menu("数据分析", LevelEnum.ONE, LevelEnum.ONE.getDescription(), "/update", "update", meta3);
            Menu menu4 = new Menu("个人中心", LevelEnum.ONE, LevelEnum.ONE.getDescription(), "/join", "join", meta4);
            Menu menu11 = new Menu("首页", LevelEnum.TWO, LevelEnum.TWO.getDescription(), "/home", "home", "view/single-page/home", meta11, menu1);
            Menu menu21 = new Menu("数字渐变", LevelEnum.TWO, LevelEnum.TWO.getDescription(), "/count_to_page", "count_to_page", "view/components/count-to/count-to.vue", meta5, menu2);
            Menu menu22 = new Menu("多功能表格", LevelEnum.TWO, LevelEnum.TWO.getDescription(), "/tables_page", "tables_page", "view/components/test/tables.vue", meta6, menu2);
            Menu menu31 = new Menu("分割窗口", LevelEnum.TWO, LevelEnum.TWO.getDescription(), "/split_pane_page", "split_pane_page", "view/components/split-pane/split-pane.vue", meta7, menu3);
            Menu menu32 = new Menu("Markdown编辑器", LevelEnum.TWO, LevelEnum.TWO.getDescription(), "/markdown_page", "markdown_page", "view/components/markdown/markdown.vue", meta8, menu3);
            menus.add(menu1);
            menus.add(menu2);
            menus.add(menu3);
            menus.add(menu4);
            menus.add(menu11);
            menus.add(menu21);
            menus.add(menu22);
            menus.add(menu31);
            menus.add(menu32);
            menuRepository.saveAll(menus);

            Set<RoleMenuRel> roleMenuRels = new HashSet<>();
            RoleMenuRel roleMenuRel = null;
            for (Menu menu : menus) {
                for (Role role : roles) {
                    roleMenuRel = new RoleMenuRel();
                    if (!RoleEnum.SUPERADMIN.name().equals(role.getName()) && "系统管理".equals(menu.getName())) {
                        continue;
                    }
                    roleMenuRel.setRole(role);
                    roleMenuRel.setMenu(menu);
                    roleMenuRels.add(roleMenuRel);
                }
            }
            roleMenuRelRepository.saveAll(roleMenuRels);

            logger.info("初始化数据完成");
        } else {
            logger.info("ddl-auto：" + ddl_auto + "不是create操作，不进行初始化数据");
        }

    }
}
